package util;

import org.testng.annotations.Test;
import org.apache.log4j.Logger;

import org.openqa.jetty.http.HttpRequest;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;

import util.TestCaseMessages;
import util.TestUtil;


import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.testng.annotations.Test;

public class XMLtoHash
{
		@Test

	   public void readall_printxml() throws ParserConfigurationException, SAXException, IOException 
		{
		  Hashtable ht= new Hashtable();
		 
		int flag;
		flag=0;

			  
			  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			 // Document doc = docBuilder.parse (new File("D:\\09012015\\Desktop\\xml1.xml")); // normalize text representation
			  Document doc = docBuilder.parse ("http://zissit2wb01.education.vic.gov.au:8082/ZIServerAdmin/XMLMessage.aspx?RecordID=378867&MessageType=AUDITMESSAGE");
			  doc.getDocumentElement ().normalize ();
			 // System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
			  NodeList list = doc.getElementsByTagName("*");
			  System.out.println(list.getLength());
		         System.out.println("XML Elements: ");
		     for (int i=0; i<list.getLength(); i++)
		       {
		        	
		           // Get element
		         Element element = (Element)list.item(i);
		        if (element.getNodeName()=="SIF_EventObject")
		        {
		        	   flag=1;
		        	  
		        }
		           if (flag==1 && (element.getChildNodes().getLength()==1))
		           {
		        	 
		        	   
		           System.out.println(i+"."+"---"+  "  "+ element.getTagName()+ "  " + element.getTextContent());
		           // doc.getElementsByTagName(element.getTagName()).item(0).getTextContent() or use  element.getTextContent() 
		        	ht.put(element.getTagName(), element.getTextContent()); 
		        	
		        	   if (element.getAttributes().getLength()>0)
		        	   {
		        		   for ( int l=element.getAttributes().getLength()-1; l>=0; l--)
			        	     {
			        		   System.out.print(" Attr       " +l+".  "+ element.getAttributes().item(l).getNodeName()+ " : " +   element.getAttributes().item(l).getTextContent());
			        		   ht.put(element.getTagName()+" Attr: "+element.getAttributes().item(l).getNodeName(), element.getAttributes().item(l).getTextContent()); 
			        	     }
		        	   }
		        		   
		           
		           }
		           
		           else
		           {
		        	   if(flag==1)
		        	   {
		        	   System.out.println(element.getTagName()+ "  attrbs: count "   +  element.getAttributes().getLength());
		        	   int len=element.getAttributes().getLength();
		        	   if (len>0) {
		        	   System.out.println(" ---------------------------");
		        	   
		        	   for ( int j=len-1; j>=0; j--)
		        	     {
		        		   System.out.print("  Attr     " +j+".  "+ element.getAttributes().item(j).getNodeName()+ " : " +   element.getAttributes().item(j).getTextContent());
		        		   ht.put(element.getTagName()+" Attr: "+element.getAttributes().item(j).getNodeName(), element.getAttributes().item(j).getTextContent()); 
		        	     }
		        	   System.out.println(" ---------------------------");
		        	   }
		        	   }
		           }
		        
		     }
		     System.out.println(" now from ht");
		     System.out.println(ht);
		}
		      

}





